$(function () {
    //导出excel表格
    $("#export_excel").click(function () {
        var gatewayId = $("#gatewayId").val();
        var head = [
            '<table>',
            '<tr>',
            '<td style="height:50px;font-size:24px;font-weight:bold;text-align:center;" colspan="' + $("#gateway_table thead tr td").length + '">公司用电统计表</td>',
            '</tr>',
            '</table>'
        ];
        head = head.join('');
        var setting = [
            '<table>',
            '<tr>',
            '<td colspan="2" style="height:30px;font-size:16px;text-align:center;">楼层/车间</td>',
            '<td colspan="3"></td>',
            '<td></td>',
            '<td colspan="2" style="height:30px;font-size:16px;text-align:center;">网关</td>',
            '<td colspan="3" style="height:30px;font-size:16px;text-align:center;">' + gatewayId.toString() + '</td>',
            '</tr>',
            '<tr>',
            '<td colspan="2" style="height:30px;font-size:16px;text-align:center;">3G卡号</td>',
            '<td colspan="3"></td>',
            '<td></td>',
            '<td colspan="2"></td>',
            '<td colspan="3"></td>',
            '</tr>',
            '<tr>',
            '</tr>',
            '</table>'
        ];
        setting = setting.join('');
        tableToExcel('gateway_table', head + setting, '公司用电统计表', $("#gatewayId").val() + "_" + $("#searchDate").val());
    });

    //执行查询操作
    $("#searchEnergy").click(function () {
        var gateway = $("#gatewayId").val();
        if (gateway == '') {
            $("#errorMessage").html("请输入网关号！");
        } else {
            $("#errorMessage").html("");
            var param = {
                "companyCode": $("#companyCode").val(),
                "searchDate": $("#searchDate").val(),
                "gatewayId": $("#gatewayId").val()
            };
            $.ajax({
                contentType: "application/json",
                url: "getGatewayDetail.shtml",
                data: param,
                async: false,
                dataType: "json",
                success: function (data) {
                    if (data.length > 0) {
                        $("#noDataMessage").html("");
                        var points = _.sortBy(_.uniq(_.pluck(data, 'point')));
                        var dates = _.sortBy(_.uniq(_.pluck(data, 'summarydate')));
                        $("#gateway_table thead tr").html("<td>序号</td><td>日期</td>");
                        _.each(points, function (item) {
                            $("#gateway_table thead tr").append("<td>" + item + "</td>");
                        });
                        $("#gateway_table tbody").html("");
                        _.each(dates, function (item, num) {
                            var tbody = "<td>" + (num + 1) + "</td><td>" + item + "</td>";
                            var filters = _.filter(data, function (filter) {
                                return filter.summarydate == item;
                            });
                            _.each(points, function (point) {
                                var target = _.find(filters, function (filter) {
                                    return filter.point == point;
                                });
                                if (target && target.power) {
                                    tbody = tbody + "<td class=" + point + ">" + target.power.toFixed(2) + "</td>";
                                } else {
                                    tbody = tbody + "<td class=" + point + "> 0.00 </td>";
                                }
                            });
                            $("#gateway_table tbody").append("<tr>" + tbody + "</tr>");
                        });
                        $("#gateway_table tbody").append("<tr><td colspan='2'>合计</td></tr>");
                        _.each(points, function (point) {
                            var datas = [];
                            $('.' + point).each(function () {
                                datas.push(parseFloat($(this).text().trim()));
                            });
                            var sum = _.reduce(datas, function (memo, num) {
                                return memo + num;
                            }, 0);
                            $("#gateway_table tbody tr:last").append("<td>" + parseFloat(sum).toFixed(2) + "</td>");
                        })
                    }else{
                        $("#gateway_table thead tr").html("");
                        $("#gateway_table tbody").html("");
                        $("#noDataMessage").html("暂无数据");
                    }
                }
            });
        }
    });


});
